package com.example.lowbird.lowbird.adapter;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.daimajia.slider.library.Animations.DescriptionAnimation;
import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.daimajia.slider.library.SliderTypes.TextSliderView;
import com.daimajia.slider.library.Tricks.ViewPagerEx;
import com.example.lowbird.lowbird.R;
import com.example.lowbird.lowbird.app.Myapp;
import com.example.lowbird.lowbird.bean.Recommendbean;
import com.example.lowbird.lowbird.bean.SlideBean;
import com.example.lowbird.lowbird.constant.Contants;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Administrator on 2016/10/31.
 */
public class Myrvadapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE1 = 1;
    private static final int TYPE2 = 2;
    private static final int TYPE0 = 0;
    private static Context mContext;
    private LayoutInflater inflater;
    private Recommendbean bean;
    private List<Recommendbean.DataBean> data;
    private Slideholder holders;
    private static BaseSliderView.OnSliderClickListener sllistenser;
    private boolean flag = true;
    private SliderLayout mSl_host;

    public Myrvadapter(Recommendbean bean, Context mContext) {
        this.bean = bean;
        this.mContext = mContext;
        inflater = LayoutInflater.from(mContext);
        if (bean.getData() != null) {
            data = bean.getData();
        } else {
            data = new ArrayList<>();
        }
        sllistenser = new BaseSliderView.OnSliderClickListener() {


            @Override
            public void onSliderClick(BaseSliderView slider) {
                //TODO 跳转事件
                Log.e("TAG", "onSliderClick: click");
            }
        };

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE0) {
            return new Slideholder(inflater.inflate(R.layout.adapter_host_slide, null));
        } else if (viewType == TYPE1) {
            return new Myviewholder(inflater.inflate(R.layout.adapter_hostrv, null));
        } else {
            return new Myviewholder(inflater.inflate(R.layout.adapter_hostrv2, null));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        if (position == 0) {
            holders = (Slideholder) holder;
            if (flag) {
                setSlideholder(holders, position);

                flag = false;
            }

        } else if (position >= 1) {
            Myviewholder holder1 = (Myviewholder) holder;
            setMyholder(holder1, position);

        }

    }

    private void setSlideholder(Slideholder holders, int position) {

        mSl_host = holders.sliderLayout;
        initimages(mSl_host);

    }

    //加载头部轮播
    public static void initimages(final SliderLayout mSl_host) {
        Request request = new Request.Builder().get().url(Contants.API.HOST_SLIDLAYOUT).build();
        Myapp.okhttpclient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                String s = response.body().string();
                s = "{'data':" + s + "}";

                SlideBean slideBean = new Gson().fromJson(s, SlideBean.class);
                final List<SlideBean.DataBean> data = slideBean.getData();


                mSl_host.post(new Runnable() {
                    @Override
                    public void run() {
                        for (int i = 0; i < data.size(); i++) {

                            mSl_host.post(new Runnable() {
                                @Override
                                public void run() {

                                }
                            });
                            String title = data.get(i).getName();
                            TextSliderView textSliderView = new TextSliderView(mContext);
                            textSliderView.description(title).image(data.get(i).getImgUrl())
                                    .setScaleType(BaseSliderView.ScaleType.Fit).setOnSliderClickListener(sllistenser);
                            mSl_host.addSlider(textSliderView);

                        }

                        mSl_host.setPresetTransformer(SliderLayout.Transformer.Accordion);
                        mSl_host.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
                        mSl_host.setCustomAnimation(new DescriptionAnimation());
                        mSl_host.setDuration(4000);
                        mSl_host.addOnPageChangeListener(new ViewPagerEx.OnPageChangeListener() {
                            @Override
                            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                            }

                            @Override
                            public void onPageSelected(int position) {
                                Log.e("TAG", "onPageSelected: " + position);
                            }

                            @Override
                            public void onPageScrollStateChanged(int state) {

                            }
                        });


                    }
                });


            }
        });
    }

    private void setMyholder(Myviewholder holder1, final int position) {

        holder1.textView.setText(data.get(position).getTitle());
        Log.e("TAG", "onBindViewHolder: " + data.get(position).getCpOne().getImgUrl());
        Glide.with(mContext).load(data.get(position).getCpOne().getImgUrl()).diskCacheStrategy(DiskCacheStrategy.ALL)
                .crossFade()
                .into(holder1.imageview1);

        Log.e("TAG", "onBindViewHolder: " + position);
        Glide.with(mContext).load(data.get(position).getCpTwo().getImgUrl()).diskCacheStrategy(DiskCacheStrategy.ALL)
                .crossFade()
                .into(holder1.imageview2);

        Glide.with(mContext).load(data.get(position).getCpThree().getImgUrl()).diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.mipmap.ic_launcher)
                .crossFade()
                .into(holder1.imageview3);


        View.OnClickListener listenser = new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                switch (v.getId()) {

                    case R.id.imageview1:
                        Log.e("TAG", "onClick: " + position + data.get(position).getCpOne().getTitle());
                        break;

                    case R.id.imageview2:
                        Log.e("TAG", "onClick: " + position + data.get(position).getCpTwo().getTitle());
                        break;
                    case R.id.imageview3:
                        Log.e("TAG", "onClick: " + position + data.get(position).getCpThree().getTitle());
                        break;
                    default:
                        break;
                }

            }
        };

    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {

            return TYPE0;
        } else {
            if (position % 2 == 0) {
                return TYPE1;
            }
            return TYPE2;
        }
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    public void addrecomend(Recommendbean recommendbean) {
        List<Recommendbean.DataBean> data1 = recommendbean.getData();
        this.data.addAll(data1);
        notifyDataSetChanged();

    }


    class Myviewholder extends RecyclerView.ViewHolder {
        private ImageView imageview1;
        private ImageView imageview2;
        private ImageView imageview3;

        private ObjectAnimator obj;
        private TextView textView;


        public Myviewholder(View itemView) {

            super(itemView);
            itemView.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            textView = ((TextView) itemView.findViewById(R.id.text_title));
            imageview1 = ((ImageView) itemView.findViewById(R.id.imageview1));

            imageview2 = ((ImageView) itemView.findViewById(R.id.imageview2));

            imageview3 = ((ImageView) itemView.findViewById(R.id.imageview3));
            //声明点击事件


            View.OnClickListener listenser = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final int position = getLayoutPosition();
                    if (callback != null) {
                        switch (v.getId()) {


                            case R.id.imageview1:


                                initanimation(imageview1, data.get(position).getCpOne().getId());
                                break;
                            case R.id.imageview2:

                                initanimation(imageview2, data.get(position).getCpTwo().getId());
                                break;
                            case R.id.imageview3:

                                initanimation(imageview3, data.get(position).getCpThree().getId());
                                break;
                            default:
                                break;
                        }
                    }
                }

                private void initanimation(ImageView imageview, final int id) {


                    obj = ObjectAnimator.ofFloat(imageview, "RotationX", -45, 90, -30, 45,0);

                    obj.setDuration(500);

                    obj.addListener(new Animator.AnimatorListener() {
                        @Override
                        public void onAnimationStart(Animator animation) {

                        }

                        @Override
                        public void onAnimationEnd(Animator animation) {
                            callback.gogoods(id);
                        }

                        @Override
                        public void onAnimationCancel(Animator animation) {

                        }

                        @Override
                        public void onAnimationRepeat(Animator animation) {

                        }
                    });
                    obj.start();
                }


            };

            imageview1.setOnClickListener(listenser);
            imageview2.setOnClickListener(listenser);
            imageview3.setOnClickListener(listenser);


        }
    }


    class Slideholder extends RecyclerView.ViewHolder {

        private SliderLayout sliderLayout;

        public Slideholder(View itemView) {
            super(itemView);

            sliderLayout = ((SliderLayout) itemView.findViewById(R.id.sl_host));
        }
    }


    public interface Hostinterface {
        public void gogoods(int id);


    }

    public Hostinterface callback;

    public Hostinterface getCallback() {
        return callback;
    }

    public void setCallback(Hostinterface callback) {
        this.callback = callback;
    }
}
